/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.digest;

import cn.seaboot.commons.file.IOUtils;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 *
 * @author Mr.css 2018-5-23 10:20:58
 */
public class MD5 {
    private MD5() {
    }

    private static final String MESSAGE_DIGEST_MD5 = "MD5";
//    public static final String MESSAGE_DIGEST_MD2 = "MD2";
//    public static final String MESSAGE_DIGEST_SHA = "SHA";
//    public static final String MESSAGE_DIGEST_SHA_224 = "SHA-224";
//    public static final String MESSAGE_DIGEST_SHA_256 = "SHA-256";
//    public static final String MESSAGE_DIGEST_SHA_384 = "SHA-384";
//    public static final String MESSAGE_DIGEST_SHA_512 = "SHA-512";

    /**
     * 数据摘要
     *
     * @param data 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static byte[] digest(byte[] data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(MESSAGE_DIGEST_MD5);
            md5.update(data);
            return md5.digest();
        } catch (NoSuchAlgorithmException e) {
            //if in standard api, it never seems that cause this exception
            throw new IllegalArgumentException("no provider supports a MD5 spi ", e);
        }
    }

    /**
     * 数据摘要
     *
     * @param is 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static byte[] digest(InputStream is) throws IOException {
        try (BufferedInputStream bis = new BufferedInputStream(is)) {

            MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_MD5);

            byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];

            int bytesRead;
            while ((bytesRead = bis.read(buffer)) != -1) {
                md.update(buffer, 0, bytesRead);
            }

            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            //if in standard api, it never seems that cause this exception
            throw new IllegalArgumentException("no provider supports a MD5 spi ", e);
        }
    }


    /**
     * 32位摘要
     *
     * @param is 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static String hexBit32(InputStream is) throws IOException {
        return Hex.encodeToString(digest(is));
    }


    /**
     * 32位摘要
     *
     * @param data 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static String hexBit32(String data) {
        return Hex.encodeToString(digest(data.getBytes()));
    }

    /**
     * 16位摘要
     *
     * @param data 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static String hexBit16(String data) {
        return hexBit32(data).substring(8, 24);
    }

    /**
     * 大写的32位摘要
     *
     * @param data 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static String hexBit32Uppercase(String data) {
        return Hex.encodeToString(digest(data.getBytes())).toUpperCase();
    }

    /**
     * 大写16位摘要
     *
     * @param data 数据
     * @return result
     * @throws IllegalArgumentException when a {@link NoSuchAlgorithmException} is caught.
     */
    public static String hexBit16Uppercase(String data) {
        return hexBit32(data).substring(8, 24).toUpperCase();
    }
}
